package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Category;
import model.Myuser;
import model.Target;
import ejb.CategoryFacadeEJBRemote;
import ejb.TargetFacadeEJBRemote;
import ejb.UserFacadeEJBRemote;

/**
 * Servlet implementation class LikeList
 */
@WebServlet("/LikeList")
public class LikeList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 //@EJB CategoryFacadeEJBRemote cf;   
	@EJB TargetFacadeEJBRemote tf;
    @EJB  UserFacadeEJBRemote uf;
    @EJB CategoryFacadeEJBRemote cf;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LikeList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
        String username=(String)session.getAttribute("username");
        try{
        if(username==null){
            out.print("Please login first! redirecting to Login...");
            response.setHeader("Refresh","2;URL=login.jsp");
        }
        else{
        	Myuser user=uf.findUserByName(username);
	        List<Target> likes=tf.getAllTargetsLikedByOne(user);
	        
			//ArrayList<Target> likes=tf.getAllTargetsLikedByOne();
			request.setAttribute("likelist", likes);	
	        //request.setAttribute("test", user);
	        }
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
	        finally{
	        	request.getRequestDispatcher("likelist.jsp").forward(request, response);
	        }
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
